﻿namespace Pps.Gui.Controls
{
    using Pps.Gui.Base;

    public class Button : Control
    {
        private string text;
        private bool touched;
        private bool selected;
        private bool selectable;

        public Button()
            : this(string.Empty, 0, 0)
        {
        }

        public Button(string text, int x, int y)
            : this(text, x, y, 0, 0)
        {

        }

        public Button(string text, int x, int y, int width, int height)
            : base(x, y, width, height)
        {
            this.text = text;

            this.Clicked += TouchUp;
            this.Touched += TouchDown;
            this.LostFocus += TouchMoveOut;
        }

        public string Text
        {
            get { return this.text; }
            set
            {
                this.text = value;
                this.Invalidate();
            }
        }

        public bool Selected
        {
            get { return this.selected; }
            set
            {
                if (this.Selectable == true && this.selected != value)
                {
                    this.selected = value;
                    this.Invalidate();
                }
            }
        }

        public bool Selectable
        {
            get { return this.selectable; }
            set
            {
                if (this.selectable != value)
                {
                    this.selectable = value;
                    this.Invalidate();
                }
            }
        }

        protected override void Paint(Graphics graphics)
        {
            graphics.DrawRectangle(Color.Black, 1, this.X, this.Y, this.Width, this.Height, 0, 0, this.touched || this.selected ? Color.Black : Color.White, 0, 0, this.touched || this.selected ? Color.Black : Color.White, 0, 0, 256);
            graphics.DrawTextInRect(this.text, X, Y + (this.Height / 2) - (this.Font.Height / 2), this.Width, this.Height, Graphics.DT_AlignmentCenter, this.touched || this.selected ? Color.White : Color.Black, this.Font);
        }

        private void TouchDown()
        {
            this.touched = true;

            if (this.Selectable == false)
            {
                this.Invalidate();
            }
        }

        private void TouchUp()
        {
            this.touched = false;

            if (this.Selectable == false)
            {
                this.Invalidate();
            }
        }

        private void TouchMoveOut()
        {
            this.touched = false;

            this.Invalidate();
        }
    }
}